package top.asked.wdy_cdftx_app;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import org.json.JSONArray;
import org.json.JSONObject;

import top.asked.wdy_cdftx_app.api.JsonData;
import top.asked.wdy_cdftx_app.news.Category;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class NewsActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news);
		getJsonData();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.news, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void getJsonData() {
        JsonData api = new JsonData();
        JSONObject jArr;
        JSONArray jobj;
        JSONObject firstitem;
        try {
        	//调用 Api 接口 
        	jArr = api.getObject("news-category.php?");
        	//从返回的 json 中取出数据
            jobj = jArr.getJSONArray("list");
            GsonBuilder gsonb = new GsonBuilder();
            Gson gson = gsonb.create();
            firstitem = jobj.getJSONObject(0);
            Category category = gson.fromJson(firstitem.toString(), Category.class);

            Log.d("LOG_CAT print data", category.toString());
            ((TextView) findViewById(R.id.categoryname)).setText(category.categoryname);
            ((TextView) findViewById(R.id.parentid)).setText(category.parentid);
            ((TextView) findViewById(R.id.type_id)).setText(category.type_id);

        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), e.getMessage(),
                    Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }

}
